import { create } from 'zustand'
import { QrcodeStatusEnum, QrcodeTabEnum, QrcodeTypeEnum, QrcodeFilter, StaffQrcode, GroupQrcode } from './interface'

// 修改页面状态接口
interface QrcodePageState {
  status: QrcodeStatusEnum           // 当前状态：我的/团队
  currentTab: QrcodeTabEnum          // 当前tab：员工/群
  filter: QrcodeFilter               // 筛选条件
  staffList: StaffQrcode[]          // 员工活码列表
  groupList: GroupQrcode[]          // 群活码列表
  page: number                      // 当前页码
  pageSize: number                  // 每页数量
  total: number                     // 总数量
  isMore: boolean                   // 是否还有更多数据
  firstLoaded: boolean               // 首次加载完成
}

interface ActivityQrcodeStore extends QrcodePageState {
  // 设置当前状态（我的/团队）
  setStatus: (status: QrcodeStatusEnum) => void
  // 设置当前tab（员工/群）
  setCurrentTab: (tab: QrcodeTabEnum) => void
  // 设置筛选条件
  setFilter: (name?: string, type?: QrcodeTypeEnum) => void
  // 设置员工活码列表
  setStaffList: (list: StaffQrcode[]) => void
  // 设置群活码列表
  setGroupList: (list: GroupQrcode[]) => void
  // 设置分页信息
  setPagination: (page: number, total?: number) => void
  setFirstLoaded: (firstLoaded: boolean) => void
  // 重置状态
  reset: () => void
}

const initialState: QrcodePageState = {
  status: QrcodeStatusEnum.MY,
  currentTab: QrcodeTabEnum.STAFF,
  filter: {
    name: '',
    type: undefined
  },
  staffList: [],
  groupList: [],
  page: 1,
  pageSize: 10,
  total: 0,
  isMore: true,
  firstLoaded: false
}

const useActivityQrcodeStore = create<ActivityQrcodeStore>((set) => ({
  ...initialState,
  setStatus: (status) => set({ status }),
  setCurrentTab: (currentTab) => set({ currentTab }),
  setFilter: (name, type) => set((state) => ({
    filter: {
      ...state.filter,
      ...(name !== undefined && { name }),
      ...(type !== undefined && { type })
    }
  })),
  setStaffList: (list: StaffQrcode[]) => set({ staffList: list }),
  setGroupList: (list: GroupQrcode[]) => set({ groupList: list }),
  setPagination: (page, total) => set(() => ({
    page,
    ...(total !== undefined && { total })
  })),
  setFirstLoaded:(firstLoaded: boolean) => set({ firstLoaded }),
  reset: () => set(initialState)
}))

// 导出状态更新方法
export const setStatus = (status: QrcodeStatusEnum) =>
  useActivityQrcodeStore.setState({ page: 1, status, firstLoaded: false })

export const setCurrentTab = (currentTab: QrcodeTabEnum) =>
  useActivityQrcodeStore.setState({ page: 1, currentTab, firstLoaded: false })

export const setFilter = (name?: string, type?: QrcodeTypeEnum) =>
  useActivityQrcodeStore.setState((state) => ({
    page: 1,
    firstLoaded: false,
    filter: {
      ...state.filter,
      ...(name !== undefined && { name }),
      ...(type !== undefined && { type })
    }
  }))

export const setStaffList = (list: StaffQrcode[]) =>
  useActivityQrcodeStore.setState({ staffList: list })

export const setGroupList = (list: GroupQrcode[]) =>
  useActivityQrcodeStore.setState({ groupList: list })

export const setFirstLoaded = (firstLoaded: boolean) =>
  useActivityQrcodeStore.setState({ firstLoaded })

export const setPagination = (page: number, total?: number) =>
  useActivityQrcodeStore.setState(() => ({
    page,
    ...(total !== undefined && { total })
  }))

export const setIsMore = (isMore: boolean) =>
  useActivityQrcodeStore.setState({ isMore })

export const resetStore = () =>
  useActivityQrcodeStore.setState(initialState)

export default useActivityQrcodeStore
